package casamento.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import casamento.Bean.ClienteBean;

public class ClienteDAO {
	
	Connection connection;
	Conexao con = new Conexao();
	
	// inserir ou alterar cliente.
	public void inserir(ClienteBean clienteBean) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		session.saveOrUpdate(clienteBean);
		t.commit();

	}

	// Listar Clientes cadastrados
	public List<ClienteBean> lista() {

		List<ClienteBean> lista = new ArrayList<ClienteBean>();
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		lista = session.createQuery("FROM TbCliente").list();
		t.commit();

		return lista;

	}
	
	/**
	 * Método responsavél por alterar dados do cliente.
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * 
	 */
	public void alterarCliente(String campo, String valor, Integer id) throws ClassNotFoundException, SQLException{
		
		this.connection = con.getConnection();
		String sql = "UPDATE TbCliente SET " + campo + "=? WHERE idCliente = ?" ;
		
		PreparedStatement stmt = connection.prepareStatement(sql);
		stmt.setString(1, valor);
		stmt.setInt(2, id);
		stmt.execute();
		stmt.close();
	}

}
